var previewWin = null;
global.opener = null;
var gui = require('nw.gui');
var path = require("path");
global.opener = gui.Window.get();
//使窗口最大化
global.opener.maximize();
//关闭事件
nw.Window.get().on('close', function () {
    this.hide();
    previewWin && previewWin.close(true);
    this.close(true);
});
function openPreview(callback,args) {
    if (!previewWin) {
        //获取当前屏幕
        var curScr = gui.Screen.screens[gui.Screen.screens.length - 1];
        var wrkCX = curScr.work_area.x + curScr.work_area.width / 2;
        var wrkCY = curScr.work_area.y + curScr.work_area.height / 2;
        var opt = { position: 'center', fullscreen: true };
        opt.x = Math.round(wrkCX);
        opt.y = Math.round(wrkCY);
        gui.Window.open('preview.html', opt, function (newWin) {
            previewWin = newWin;
            newWin.on('closed', function () {
                previewWin = newWin = null;
            });
            newWin.on('document-end',function(){
                if(callback && typeof callback == 'function'){
                    callback(args);
                }
            });
        });
    }else{
        if(callback && typeof callback == 'function'){
            callback(args);
        }
    }
}
function playVerse() {
    var title = document.querySelector("h1").innerHTML;
    var verse=document.getElementById("verse").innerHTML;
    previewWin.playVerse(title, verse);
}
function playImage(img) {
    previewWin.playImage(img);
}
function playReference() {
    var title = document.querySelector("h1").innerHTML;
    var verse=referenceVerse.join('');
    previewWin.playVerse(title, verse);
}
function playHtml() {
    var title=null;
    var html = editor.txt.html();
    var matches = html.match(/<h1>(.*)<\/h1>/);

    if (matches !== null) {
        title = matches[1];
        html=html.replace(matches[0],'');
    }
    previewWin.playVerse(title, html);
}

function openItem(file){
    var execPath = path.dirname(process.execPath);
    //gui.Shell.showItemInFolder ('F:\\JiaoHui\\tt.xls');
    gui.Shell.openItem(execPath + "\\iview\\pps\\" + file);
}